package com.he.utils;

import com.he.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/7 15:20
 * @Desc
 */
public class SecurityUtils
{
    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
        Authentication authentication = getAuthentication();
        if (authentication != null && authentication.getPrincipal() instanceof LoginUser) {
            return (LoginUser) authentication.getPrincipal();
        }
        return null;
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
    * 判断是否是超级管理员
    * */
    public static Boolean isAdmin(){
        Integer id = getLoginUser().getUser().getId();
        return id != null && id.equals(1);
    }

    /**
     * 获取用户id
     */
    public static Integer getUserId() {
        LoginUser user = getLoginUser();
        return (user != null) ? user.getUser().getId() : null;
    }
}
